﻿using XYS.Core.Util.Helper;
using XYS.Service.Core.Models;

namespace XYS.Core.Models
{
    /// <summary>
    /// 全局公共设置
    /// </summary>
    public static partial class GlobalCoreSetting
    {
        private static string jWT_Issuer;
        private static string environmentName = "Unknown";
        private static string jWT_Audience;
        private static string jWT_SecretKey;
        private static TimeSpan jWT_InvalidTime;
        private static TimeSpan renewalToken_InvalidTime;

        /// <summary>
        /// 开启自动清空字符串开头的空格
        /// </summary>
        public static bool EnableStringTrimStart { get; set; } = true;
        /// <summary>
        /// 开启自动清空字符串结尾的空格
        /// </summary>
        public static bool EnableStringTrimEnd { get; set; } = true;

        /// <summary>
        /// 独立权限（独立权限由admin控制，不由总所控制）
        /// </summary>
        public static List<EnumActivateService> IndependentPermissions { get; set; } = new List<EnumActivateService>();

        /// <summary>
        /// 单端登录的服务，激活了该服务则只允许单端登录
        /// </summary>
        public static List<EnumActivateService> SingleLoginActivateService { get; set; } = new();
        /// <summary>
        /// 设备指纹校验白名单
        /// </summary>
        public static List<string> DevicePathWhiteList { get; set; } = new List<string>() {
            "/api/auth/login-check",
            "/api/auth/login-device"
        };

        /// <summary>
        /// 单端登录本地缓存时间
        /// </summary>
        public static TimeSpan TokenLocalCacheTime { get; set; } = TimeSpan.FromSeconds(10);

        /// <summary>
        /// 
        /// </summary>
        public static string JWT_Issuer
        {
            get => jWT_Issuer; set
            {
                if (jWT_Issuer != value)
                {
                    jWT_Issuer = value;
                    TokenHelper.Lssuer = value;

                    if (!TokenHelper.Lssuer.EndsWith("_" + EnvironmentName))
                        TokenHelper.Lssuer += "_" + EnvironmentName;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string JWT_Audience
        {
            get => jWT_Audience; set
            {
                if (jWT_Audience != value)
                {
                    jWT_Audience = value;
                    TokenHelper.Audience = jWT_Audience;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string JWT_SecretKey
        {
            get => jWT_SecretKey; set
            {
                if (jWT_SecretKey != value)
                {
                    jWT_SecretKey = value;
                    TokenHelper.SecurityKey = jWT_SecretKey;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static TimeSpan JWT_InvalidTime
        {
            get => jWT_InvalidTime; set
            {
                if (jWT_InvalidTime != value)
                {
                    jWT_InvalidTime = value;
                    TokenHelper.InvalidTime = jWT_InvalidTime;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static TimeSpan RenewalToken_InvalidTime
        {
            get => renewalToken_InvalidTime; set
            {
                if (renewalToken_InvalidTime != value)
                {
                    renewalToken_InvalidTime = value;
                    TokenHelper.RenewalTokenInvalidTime = renewalToken_InvalidTime;
                }
            }
        }

        /// <summary>
        /// 忽略出参为Null的字段
        /// </summary>
        public static bool ResultIgnoreNullValue { get; set; } = true;

        /// <summary>
        /// 网关地址
        /// </summary>
        public static string GatewayHost { get; set; } = "https://gateway.xysip.cn";
        /// <summary>
        /// 
        /// </summary>
        public static List<string> FormatRequestsIgnore { get; set; } = new();

        /// <summary>
        /// 
        /// </summary>
        public static string NACOS_IP { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public static string NACOS_PORT { get; set; } = "80";
        /// <summary>
        /// 
        /// </summary>
        public static string NACOS_PORT_GRPC { get; set; } = "81";


        /// <summary>
        /// 从nacos更新服务信息间隔时间
        /// </summary>
        public static TimeSpan UpdateDeayTime { get; set; } = TimeSpan.FromSeconds(5);

        /// <summary>
        /// 是否启用XXLJob
        /// </summary>
        public static bool EnableXXLJob { get; set; } = true;

        /// <summary>
        /// 当前服务名称
        /// </summary>
        public static string ServiceName { get; set; }

        /// <summary>
        /// 当前环境名称
        /// </summary>
        public static string EnvironmentName
        {
            get => environmentName; set
            {
                environmentName = value;
                if (TokenHelper.Lssuer == null)
                    TokenHelper.Lssuer = "xys_" + EnvironmentName;
                else if (!TokenHelper.Lssuer.EndsWith("_" + EnvironmentName))
                    TokenHelper.Lssuer += "_" + EnvironmentName;
            }
        }

        /// <summary>
        /// 香橼树核心库 版本号
        /// </summary>
        public static string XYSCoreDLLVersion { get; set; }

        /// <summary>
        /// 临时目录
        /// </summary>
        public static string TempPath { get; set; } = "temp";

        /// <summary>
        /// 申请的oss文件地址 有效期
        /// </summary>
        public static TimeSpan OSSExpiration { get; set; } = TimeSpan.FromHours(1);

        /// <summary>
        /// RSA 公私钥
        /// </summary>
        public static RSAKeySetting RSAKeySetting { get; set; } = new RSAKeySetting();

        /// <summary>
        /// 远程上传文件 配置
        /// </summary>
        public static RemotePutSetting RemotePutSetting { get; set; } = new RemotePutSetting();

        /// <summary>
        /// 事件总线 配置
        /// </summary>
        public static EventBugSetting EventBugSetting { get; set; } = new EventBugSetting();
        /// <summary>
        /// API请求日志 配置
        /// </summary>
        public static APIRequestLogSetting APIRequestLogSetting { get; set; } = new();
        /// <summary>
        /// MQ 配置
        /// </summary>
        public static MQSetting MQSetting { get; set; } = new();
        /// <summary>
        /// Api 日志记录者配置
        /// </summary>
        public static ApiLoggerSetting ApiLoggerSetting { get; set; } = new();
        /// <summary>
        /// http 日志记录者配置
        /// </summary>
        public static HttpLoggerSetting HttpLoggerSetting { get; set; } = new();
    }


    /// <summary>
    /// MQ 配置
    /// </summary>
    public class MQSetting
    {
        /// <summary>
        /// 记录日志的index名字
        /// </summary>
        public string ElasticName { get; set; } = "event_bus";
        /// <summary>
        /// 需要记录日志的队列名称
        /// </summary>
        public List<string> QueueNameList { get; set; } = new List<string>();
    }

    /// <summary>
    /// API请求日志配置
    /// </summary>
    public class APIRequestLogSetting
    {
        /// <summary>
        /// 是否进入MQ
        /// 如果进入MQ则需要启动消费者进行消费
        /// </summary>
        public bool UseMQ { get; set; } = false;
        /// <summary>
        /// 是否打印debug日志
        /// </summary>
        public bool Debug { get; set; } = true;
        /// <summary>
        /// 处理日志多线程并发数
        /// </summary>
        public int LogParallelForCount { get; set; } = 1;
        /// <summary>
        /// 如果进入MQ，则该配置设置进入MQ的队列名字
        /// </summary>
        public string APIRequestLogQuqueName { get; set; } = "queue-api-log";
        /// <summary>
        /// 日志黑名单，该路由下的日志不会记录日志
        /// </summary>
        public List<string> UrlBlackList { get; set; } = new();
    }

    /// <summary>
    /// 公私钥配置
    /// </summary>
    public class RSAKeySetting
    {
        /// <summary>
        /// 私钥
        /// </summary>
        public string PrivateKey { get; set; }
        /// <summary>
        /// 私钥XML格式
        /// </summary>
        public string PrivateKey_XML { get; set; }

        /// <summary>
        /// 公钥
        /// </summary>
        public string PublicKey { get; set; }
        /// <summary>
        /// 公钥XML格式
        /// </summary>
        public string PublicKey_XML { get; set; }
    }

    /// <summary>
    /// 远程上传文件 配置
    /// </summary>
    public class RemotePutSetting
    {
        /// <summary>
        /// 远程文件下载临时存放目录
        /// </summary>
        public string TempFilePath { get; set; } = "files/temp/remote";
    }


    /// <summary>
    /// 事件总线配置
    /// </summary>
    public class EventBugSetting
    {
        /// <summary>
        /// 是否启用事件总线
        /// </summary>
        public bool Enable { get; set; } = false;
        /// <summary>
        /// 记录日志的index名字
        /// </summary>
        public string ElasticName { get; set; } = "event_bus";
        /// <summary>
        /// 需要记录日志的事件名称
        /// </summary>
        public List<string> LogEventNameList { get; set; } = new List<string>();
        /// <summary>
        /// 记录触发日志的index名字
        /// </summary>
        public string ElasticNamePublish { get; set; } = "event_bus_publish";
        /// <summary>
        /// 需要记录日志的触发事件的名称
        /// </summary>
        public List<string> LogPublishEventNameList { get; set; } = new List<string>();
        /// <summary>
        /// mq host
        /// </summary>
        public string Host { get; set; }
        /// <summary>
        /// mq port
        /// </summary>
        public int Port { get; set; } = 5672;
        /// <summary>
        /// mq 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// mq 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 交换器名字
        /// </summary>
        public string ExchangeName { get; set; } = "xys_event_bus";
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 5;
        /// <summary>
        /// 事件黑名单(在这个黑名单中的事件不会触发)
        /// </summary>
        public List<string> BlackEventNameList { get; set; } = new List<string>();
    }

}

